﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Web.UI.WebControls;
using System.Net;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;
using System.IO;
using log4net;
using System.Runtime.InteropServices;
using log4net.Config;
using System.Threading;

namespace subtitle_modifier
{
    public partial class Form1 : Form
    {


     
    
        private static readonly ILog log = LogManager.GetLogger(typeof(Form1));
        private static Form1 instance;
        Thread modifiySubtitleCreatorThread;
        public static Form1 Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new Form1();
                    
                }
                return instance;
            }
        }
        
        public Form1()
        {
            InitializeComponent();
           // System.Media.SystemSounds.Asterisk.Play();
          //  System.Media.SystemSounds.Beep.Play();
           // System.Media.SystemSounds.Exclamation.Play();
           // System.Media.SystemSounds.Hand.Play();
            
            debugCheker();
          
        }


        private  void debugCheker()
        {
          #if DEBUG
          log.Info("run in debug mode");
          runInDebugMode();
          #else
          log.Info("run in relese mode");
          #endif
        }

        private  void runInDebugMode()
        {
            try
            {
 
               
                string goodTimeFile = "@" + ConfigFile.Instance.getPropertyFromReposetoriy("mySubPath");
                string goodLangFile = "@" + ConfigFile.Instance.getPropertyFromReposetoriy("GoodLangFile");
                if (string.IsNullOrEmpty(goodTimeFile))
                {
                    log.Warn("no file found in config.xml file");
                    return;
                }
                else
                {
                    log.Info("file to load from xml is: " + goodTimeFile);
                    run(goodLangFile,goodTimeFile);
                }
            }
            catch (Exception e)
            {

                log.Error(e);
                return;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            run(textBoxOpenGoodLanguagFile.Text, textBoxOpenGoodTimeFile.Text);
        }

        public void run(string goodLangFile, string goodTimeFile)
        {

            ModifiySubtitleCreator modifiySubtitleCreator = new ModifiySubtitleCreator(goodTimeFile, goodLangFile);
            modifiySubtitleCreatorThread = new Thread(modifiySubtitleCreator.run);
            modifiySubtitleCreatorThread.Start();
            this.button1.Enabled = false;

        }


        public void addInfo(string status)
        {
            
            string time = DateTime.Now.ToLongTimeString();
            Invoke(new Action(() => richTextBoxStatusLog.AppendText(time + "  " + status + "\n")));
            Invoke(new Action(() =>   this.richTextBoxStatusLog.ScrollToCaret()));
          
        }
         public void addInfo(string status,Color c)
        {
            Invoke(new Action(() => richTextBoxStatusLog.SelectionColor=c));
            addInfo(status);
            Invoke(new Action(() => richTextBoxStatusLog.SelectionColor = Color.Black));
        }
      
        public void replaceLastLine(string status)
        {
            int startIndex = 0;

            Invoke(new Action(() => startIndex = richTextBoxStatusLog.GetFirstCharIndexFromLine(richTextBoxStatusLog.Lines.Length - 2)));
            
            Invoke(new Action(() => richTextBoxStatusLog.Text = richTextBoxStatusLog.Text.Remove(startIndex)));
            addInfo(status);
        }

        public void makeButEnable()
        {
            Invoke(new Action(() => this.button1.Enabled=true));
          
        }
        private string getFileFullPath(string fileName)
        {
            string currentWorkingPath = Directory.GetCurrentDirectory();
            string ans = currentWorkingPath + "/../../../Common/subTitles/" + fileName;
            return @ans;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (modifiySubtitleCreatorThread!=null && modifiySubtitleCreatorThread.IsAlive)
            {
                modifiySubtitleCreatorThread.Abort();
            }
            base.OnClosing(e);

        }

        private void buttonOpenGoodTimeFile_Click(object sender, EventArgs e)
        {
            try
            {
              
                if (openFileDialogOpenGoodTimeFile.ShowDialog() == DialogResult.OK)
                {
                    string sourceFile = openFileDialogOpenGoodTimeFile.FileName;
                    textBoxOpenGoodTimeFile.Text = sourceFile;
                    ConfigFile.Instance.setPropertyToReposetoriy("mySubPath", sourceFile);
                }
            }
            catch (Exception e2)
            {

                log.Error(e2);
            }
            

        }

        private void buttonOpenGoodLanguagFile_Click(object sender, EventArgs e)
        {
            try
            {
               
                if (openFileDialogGoodLangFile.ShowDialog() == DialogResult.OK)
                {
                    string sourceFile = openFileDialogGoodLangFile.FileName;
                    textBoxOpenGoodLanguagFile.Text = sourceFile;
                    ConfigFile.Instance.setPropertyToReposetoriy("GoodLangFile", sourceFile);
                }
            }
            catch (Exception e2)
            {

                log.Error(e2);
            }
        }


    }
}

